package com.example.elmspringboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elmspringboot.mapper.AdminMapper;
import com.example.elmspringboot.domain.Admin;
import com.example.elmspringboot.service.AdminService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {
    //@Resource  先查找名字后查找类型，可以设定默认值：（name=“”）
    @Resource
    private AdminMapper adminMapper;
    @Override
    public Boolean login(String name, String password) {
        LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
        List<Admin> admin = adminMapper.selectList(wrapper.eq(name != null, Admin::getAdminName, name));
        Admin admin1 = admin.get(0);
        if(admin1==null){
            return false;
        }
        return admin1.getAdminPassword().equals(password);
    }

    @Override
    public Boolean register(String name,String password) {
        LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
        List<Admin> admins = adminMapper.selectList(wrapper.eq(name != null, Admin::getAdminName, name));
        if(admins.isEmpty()){
            Admin admin=new Admin();
            admin.setAdminName(name);
            admin.setAdminPassword(password);
            adminMapper.insert(admin);
        }
        return admins.isEmpty();
    }
}




